package com.audaque.springboot.foshanupload.security.encoder;


import cn.hutool.crypto.SecureUtil;
import com.audaque.springboot.foshanupload.core.util.encode.SpringBase64Utils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * md5是hash算法
 */
@Component
public class MyPasswordEncoder implements PasswordEncoder {

    /**
     * 对密码进行加密并返回
     */
    public String encode(CharSequence rawPassword) {
        String base64 = rawPassword.toString();
        String pw = SpringBase64Utils.decode(base64);
        String encPassword = SecureUtil.md5(pw);
        return encPassword;
    }

    /**
     * 验证密码是否正确:从MyUserDetailService.loadUserByUsername()方法中获取数据库中保存的密码与用户输入的密码进行比对
     */
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return encode(rawPassword).equals(encodedPassword);
    }
}
